﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Solution19
{
    public ListNode RemoveNthFromEnd(ListNode head, int n)
    {
        ListNode dummyhead=new ListNode();
        dummyhead.next = head;

        ListNode pre= dummyhead;
        ListNode cur= head;
        ListNode next= head;



        for (int i = 0; i < n; i++) { 
        
        
        next = next.next;
        }
        while (next != null) {
            next = next.next;
            pre = cur;
            cur = cur.next;//指向删除结点
        }



        pre.next = cur.next;
        return dummyhead.next;



    }
}
class Program19
{

    static void Main19(string[] args)
    {

        Solution19 solution = new Solution19();

        int[] head = [1, 2, 3, 4, 5];
        ListNode dummyhead = new ListNode();
        ListNode curA = dummyhead;
        for (int i = 0; i < head.Length; i++)
        {
            ListNode newA = new ListNode(head[i]);
            curA.next = newA;
            curA = newA;

        }
        ListNode res = solution.RemoveNthFromEnd(dummyhead.next, 2);
        while (res != null)
        {



            Console.WriteLine(res.val);
            res = res.next;
        }


    }
}
